package com.rem.designPattern.Composite组合;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合模式 将对象组合成树型结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性
 * <p>
 * 定义一个组织的抽象类以及组织下各个节点
 *
 * @author Rem
 * @date 2022-11-17
 */
public abstract class Organization {
    private String name;

    public Organization(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void add(Organization organizationComponent) {
        throw new UnsupportedOperationException();
    }

    public void remove(Organization organizationComponent) {
        throw new UnsupportedOperationException();
    }

    /**
     * 打印组织及组织下所有节点
     */
    public abstract void print();
}

class University extends Organization {
    List<Organization> colleges = new ArrayList<>();

    public University(String name) {
        super(name);
    }


    @Override
    public void add(Organization organizationComponent) {
        colleges.add(organizationComponent);
    }

    @Override
    public void remove(Organization organizationComponent) {
        colleges.remove(organizationComponent);
    }

    @Override
    public void print() {
        System.out.println("=============" + getName() + "=============");
        colleges.forEach(Organization::print);
    }
}

class College extends Organization {
    List<Organization> subjects = new ArrayList<>();

    public College(String name) {
        super(name);
    }

    @Override
    public void add(Organization organizationComponent) {
        subjects.add(organizationComponent);
    }

    @Override
    public void remove(Organization organizationComponent) {
        subjects.remove(organizationComponent);
    }


    @Override
    public void print() {
        System.out.println("=============" + getName() + "=============");
        subjects.forEach(Organization::print);
    }
}

class Subject extends Organization {

    public Subject(String name) {
        super(name);
    }

    @Override
    public void print() {
        System.out.println(getName());
    }
}